<?php

namespace app\adminabc\controller;

use app\adminabc\controller\Base;

class Recharge extends Base {
	function index() {
		$type = $this->request->param ( 'type' ) == '' ? $this->request->param ( 'type' ) : $this->request->param ( 'type' );
		$kw = $this->request->param ( 'kw' ) == '' ? $this->request->param ( 'kw' ) : $this->request->param ( 'kw' );
		$status = $this->request->param ( 'status' );
		if ($kw != '') {
			switch ($type) {
				case '1' :
					$map [] = [ 'id','=',$kw ];
					break;
				case '2' :
					$map [] = [ 'user_id','=',$kw ];
					break;
				case '3' :
					$map [] = [ 'union_id','=',$kw ];
					break;
			}
		}
		if ($status != '')
			$map [] = [ 'r.status','=',$status ];
		$data = model ( 'Recharge' )->getlist ( $map );
		$this->assign ( 'data', $data );
		return $this->fetch ();
	}
	function info() {
		$id = $this->request->param ( 'id' );
		$info = model ( 'Recharge' )->getInfo ( array ([ 'id','=',$id ] ) );
		if ($this->request->isPost ()) {
			if ($info ['status'] == 1)
				$this->error ( '该订单已完成' );
			else {
				$status = $this->request->param ( 'status' );
				$data ['status'] = $status;
				$data ['remark'] = $this->request->param ( 'remark' );
				$log = model ( 'Recharge' )->edit ( array ([ 'id','=',$id ] ), $data );
				if ($log > 0) {
					$money = $info ['money'];
					if ($status == 1 && $money > 0) {
						$info = model ( 'Member' )->getInfo ( array ([ 'user_id','=',$info ['user_id'] ] ) );
						model ( 'Member' )->incMoney ( array ([ 'user_id','=',$info ['user_id'] ] ), $money );
						unset ( $data );
						$data ['user_id'] = $this->uid;
						$data ['money'] = $money;
						$data ['create_time'] = time ();
						$data ['type'] = 1;
						$data ['before_money'] = $info ['money'];
						$data ['after_money'] = $info ['money'] + $money;
						$data ['remark'] = 'recharge';
						model ( 'Member' )->addMoneyLog ( $data );
					}
					$this->success ( '保存成功' );
				} else
					$this->error ( '保存失败' );
			}
		} else {
			$this->assign ( 'info', $info );
			return $this->fetch ();
		}
	}
	function edit() {
		$id = $this->request->param ( 'id' );
		$info = model ( 'Recharge' )->getInfo ( array ([ 'id','=',$id ] ) );
		if ($this->request->isPost ()) {
			if ($info ['status'] == 1)
				echo json_encode ( array ('code' => 0,'msg' => '该订单已完成' ) );
			else {
				$status = $this->request->param ( 'status' );
				$data ['status'] = $status;
				$data ['remark'] = $this->request->param ( 'remark' );
				$log = model ( 'Recharge' )->edit ( array ([ 'id','=',$id ] ), $data );
				if ($log > 0) {
					$money = $info ['money'];
					if ($status == 1 && $money > 0) {
						$info = model ( 'Member' )->getInfo ( array ([ 'user_id','=',$info ['user_id'] ] ) );
						model ( 'Member' )->incMoney ( array ([ 'user_id','=',$info ['user_id'] ] ), $money );
						unset ( $data );
						$data ['user_id'] = $this->uid;
						$data ['money'] = $money;
						$data ['create_time'] = time ();
						$data ['type'] = 1;
						$data ['before_money'] = $info ['money'];
						$data ['after_money'] = $info ['money'] + $money;
						$data ['remark'] = 'recharge';
						model ( 'Member' )->addMoneyLog ( $data );
					}
					echo json_encode ( array ('code' => 99,'msg' => '操作成功' ) );
				} else
					echo json_encode ( array ('code' => 0,'msg' => '操作失败' ) );
			}
		}
	}
}

?>